cmake_minimum_required(VERSION 3.10)

project(Modbus_Serial_Port)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 静态库输出到lib目录下
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
# 可执行文件输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/bin)

# 查找线程库
find_package(Threads REQUIRED)

# 查找 paho-MQTT C 库
# COMPONENTS 指定需要的组件
find_package(eclipse-paho-mqtt-c REQUIRED )

# 查找 paho-MQTT C++ 库
find_package(PahoMqttCpp REQUIRED)


# 添加各模块的子目录
add_subdirectory(src/mqtt_client)

# 合并所有模块源码，生成静态库
add_library(publish_subscribe STATIC
    ${MQTT_CLIENT_SRC}
)

# 包含头文件目录（让库和可执行文件能找到头文件）
target_include_directories(publish_subscribe PUBLIC
    ${PROJECT_SOURCE_DIR}/include
)

# 将库链接到publish_subscribe
target_link_libraries(publish_subscribe PUBLIC
    PahoMqttCpp::paho-mqttpp3          # C++ 库目标
    eclipse-paho-mqtt-c::paho-mqtt3as  # C 库目标
    Threads::Threads                   # 线程库目标
)

# 编译
add_executable(publish_test src/test/publish_test.cc)
target_link_libraries(publish_test publish_subscribe)

add_executable(subscribe_test src/test/subscribe_test.cc)
target_link_libraries(subscribe_test publish_subscribe)




# # 配置 MQTT 库相关路径（确保能找到 MQTT 头文件和库文件）
# set(MQTT_INCLUDE_DIR /usr/local/include)  # MQTT 头文件安装路径（默认）
# set(MQTT_LIB_DIR /usr/local/lib)          # MQTT 库文件安装路径（默认）
# set(MQTT_LIBS paho-mqttpp3 paho-mqtt3as ssl crypto)   # MQTT 依赖库：C++ 库 + C 库
# set(THREAD_LIB pthread)                   # 线程库（适配 std::thread）
